package com.singing.dao;

import com.singing.model.Singer;
import com.singing.util.DbUtil;

import java.sql.*;
import java.util.List;
import java.util.stream.Collectors;

public class SingerDao {

    // 根据ID查询歌手
    public Singer selectById(Integer id) {
        Connection connection = DbUtil.getConnection();
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        try {
            ps = connection.prepareStatement("select * from singer where id=?");
            ps.setInt(1, id);
            resultSet = ps.executeQuery();
            Singer singer = null;
            if (resultSet.next()) {
                singer = new Singer();
                singer.setId(resultSet.getInt("id"));
                singer.setName(resultSet.getString("name"));
                singer.setGender(resultSet.getInt("gender"));
            }
            return singer;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtil.closeResource(connection, ps, resultSet);
        }
        return null;
    }

    // 添加歌手
    public int insert(Singer singer) {
        Connection connection = DbUtil.getConnection();
        PreparedStatement ps = null;
        try {
            ps = connection.prepareStatement(
                    "insert into singer(name, gender) values(?,?)",
                    Statement.RETURN_GENERATED_KEYS);
            ps.setString(1, singer.getName());
            ps.setInt(2, singer.getGender());
            int result = ps.executeUpdate();

            try (ResultSet rs = ps.getGeneratedKeys()) {
                if (rs.next()) {
                    int generatedId = rs.getInt(1);
                    singer.setId(generatedId);
                }
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DbUtil.closeResource(connection, ps);
        }
        return 0;
    }

    // 更新歌手信息
    public int update(Singer singer) {
        Connection connection = DbUtil.getConnection();
        PreparedStatement ps = null;
        try {
            ps = connection.prepareStatement(
                    "update singer set name=?, gender=? where id=?");
            ps.setString(1, singer.getName());
            ps.setInt(2, singer.getGender());
            ps.setInt(3, singer.getId());
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtil.closeResource(connection, ps);
        }
        return 0;
    }

    // 删除歌手
    public int deleteById(Integer id) {
        Connection connection = DbUtil.getConnection();
        PreparedStatement ps = null;
        try {
            ps = connection.prepareStatement("delete from singer where id=?");
            ps.setInt(1, id);
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtil.closeResource(connection, ps);
        }
        return 0;
    }

    // 查询所有歌手,返回list
    public List<Singer> selectAll() {
        Connection connection = DbUtil.getConnection();
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        try {
            ps = connection.prepareStatement("select * from singer");
            resultSet = ps.executeQuery();
            java.util.ArrayList<Singer> list = new java.util.ArrayList<>();
            while (resultSet.next()) {
                Singer singer = new Singer();
                singer.setId(resultSet.getInt("id"));
                singer.setName(resultSet.getString("name"));
                singer.setGender(resultSet.getInt("gender"));
                list.add(singer);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtil.closeResource(connection, ps, resultSet);
        }
        return null;
    }


    //根据id批量查询
    public List<Singer> selectByIds(List<Integer> ids) {
        Connection connection = DbUtil.getConnection();
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        String idsStr = ids.stream().map(String::valueOf).collect(Collectors.joining(","));
        try {
            ps = connection.prepareStatement(String.format("select * from singer where id in (%s)", idsStr));
            resultSet = ps.executeQuery();
            java.util.ArrayList<Singer> list = new java.util.ArrayList<>();
            while (resultSet.next()) {
                Singer singer = new Singer();
                singer.setId(resultSet.getInt("id"));
                singer.setName(resultSet.getString("name"));
                singer.setGender(resultSet.getInt("gender"));
                list.add(singer);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtil.closeResource(connection, ps, resultSet);
        }
        return null;
    }
}